package com.jeeplus.modules.monitor.utils;


import com.fasterxml.jackson.databind.ObjectMapper;
 import com.jeeplus.modules.monitor.model.Message;


import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.log4j.Logger;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;



public class Business {
    public static Achieve achieve = null;
    public static ObjectMapper jsonMapper = null;
    private static Error error;
     public static Logger logs = Logger.getLogger(Business.class);

    static {
        getError();
        getAchieve();
        getObjectMapper();
    }

     /**
     * 单例
     *
     * @return
     */
    private static Achieve getAchieve() {
        if (achieve == null) {
            synchronized (Achieve.class) {
                if (achieve == null) {
                    achieve = new Achieve();
                }
            }
        }
        return achieve;
    }

    /**
     * 单例
     *
     * @return
     */
    private static Error getError() {
        if (error == null) {
            synchronized (Error.class) {
                if (error == null) {
                    error = new Error();
                }
            }
        }
        return error;
    }

    /**
     * 单例
     *
     * @return
     */
    private static ObjectMapper getObjectMapper() {
        if (jsonMapper == null) {
            synchronized (ObjectMapper.class) {
                if (jsonMapper == null) {
                    jsonMapper = new ObjectMapper();
                }
            }
        }
        return jsonMapper;
    }

    /**
     * 解析json数据请求信息 转换为制定类型
     *
     * @param jsonStr json格式字符串
     * @param obj     所需转换的类型
     * @return
     */
    public static <T> Object JSONToObj(String jsonStr, Class<T> obj) {
        Object t = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            t = objectMapper.readValue(jsonStr, obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 将类转换为json
     *
     * @param obj
     * @return
     * @throws JSONException
     * @throws IOException
     */
    public static <T> JSONObject objectToJson(T obj) throws
            IOException {
        ObjectMapper mapper = new ObjectMapper();

        String jsonStr = "";
        try {
            jsonStr = mapper.writeValueAsString(obj);
        } catch (IOException e) {
            throw e;
        }
        return JSONObject.fromObject(jsonStr);
    }


    /***
     * 成功返回信息
     *
     * @param code 成功码
     * @return
     */
    public Message<Object> returnGetMessage(int code) {
        return achieve.getMessage(null, code, "SUCCESS");
    }


    /**
     * @return
     */
    public Message<Object> returnGetMessage(Object data, int code) {
        System.out.println(achieve.getMessage(data, code, "SUCCESS").toString());
        return achieve.getMessage(data, code, "SUCCESS");
    }


    /**
     * 请求失败返回错误
     *
     * @param code 错误码
     * @return
     */
    public String returnGetError(int code) {
        return achieve.getError(code, com.jeeplus.modules.monitor.model.Error.getError(code));
    }


    /**
     * 将类型转换为MAP
     *
     * @param obj
     * @return
     */
    public Map<String, Object> getMap(Object obj) {
        Map params = new HashMap(0);
        try {
            PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
            PropertyDescriptor[] descriptors = propertyUtilsBean
                    .getPropertyDescriptors(obj);
            for (int i = 0; i < descriptors.length; i++) {
                String name = descriptors[i].getName();
                if (!"class".equals(name))
                    params.put(name,
                            propertyUtilsBean.getNestedProperty(obj, name));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return params;
    }





}
